/**
 * PapervisionScene
 * Basic Papervision3D scene with camera, viewport, and renderer
 *
 * @author		Craig Phares
 * @version		2.0
 */
package com.rad.papervision3d {
	
	import flash.display.Sprite;
	import flash.events.Event;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.render.QuadrantRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.render.BasicRenderEngine;

	public class PapervisionScene extends Sprite {
		
		private var _scene:Scene3D;						// the 3d scene
		private var _cameraPivot:DisplayObject3D;		// the origin (0, 0, 0)
		private var _camera:Camera3D;					// the main camera
		private var _viewport:Viewport3D;				// the viewport
		private var _renderer:BasicRenderEngine;		// basic renderer (faster)
		private var _quadRenderer:QuadrantRenderEngine;	// quad renderer (better)
		private var _renderAsQuad:Boolean;				// flag to determine render method
		
		/**
		 * Creates an instance of the PapervisionScene class inside a Sprite.
		 *
		 * @param viewportWidth		the width of the viewport - defaults to 800
		 * @param viewportHeight	the height of the viewport - defaults to 600
		 * @param autoScaleToStage	automatically scale the viewport to the stage
		 */
		public function PapervisionScene(viewportWidth:int = 800, viewportHeight:int = 600, autoScaleToStage:Boolean = false) {
			
			// default to quad rendering for accuracy
			_renderAsQuad = true;
			
			// create the objects
			_scene = new Scene3D();
			_cameraPivot = new DisplayObject3D();
			_scene.addChild(_cameraPivot);
			_camera = new Camera3D();
			_viewport = new Viewport3D(viewportWidth, viewportHeight, autoScaleToStage, true, true, true);
			_renderer = new BasicRenderEngine();
			_quadRenderer = new QuadrantRenderEngine(QuadrantRenderEngine.CORRECT_Z_FILTER);
			
			// add the viewport to the stage
			addChild(_viewport);
			
			// render the scene every new frame
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
		}
		
		// getters and setters
		public function get scene():Scene3D { return _scene; }
		public function get camera():Camera3D { return _camera; }
		public function get viewport():Viewport3D { return _viewport; }
		public function get cameraPivot():DisplayObject3D { return _cameraPivot; }
		
		public function get renderAsQuad():Boolean { return _renderAsQuad; }		
		public function set renderAsQuad(r:Boolean):void { _renderAsQuad = r; }
		
		/**
		 * add an object to the 3d scene
		 *
		 * @param o		the object to add to the Papervision3D scene
		 */
		public function addToScene(o:DisplayObject3D):void {
			_scene.addChild(o);
		}
		
		/**
		 * render the scene to the stage
		 *
		 * @param e		the enterframe event
		 */
		private function onEnterFrame(e:Event):void {
			if (_renderAsQuad) {
				_quadRenderer.renderScene(_scene, _camera, _viewport);
			} else {
				_renderer.renderScene(_scene, _camera, _viewport);
			}
		}
		
	}

}